Skip to content

Method: FieldStrategy(EntityType, FieldSpecification, Descriptor, EntityMappingHelper)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom;
16:
17: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.*;
19: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
20: import cz.cvut.kbss.ontodriver.exception.NotYetImplementedException;
21: import cz.cvut.kbss.ontodriver.model.Assertion;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23:
24: import java.net.URI;
25:
26: abstract class FieldStrategy<T extends FieldSpecification<? super X, ?>, X> {
27:
28: final EntityType<X> et;
29: final T attribute;
30: final Descriptor attributeDescriptor;
31: final EntityMappingHelper mapper;
32: CascadeResolver cascadeResolver;
33:
34: FieldStrategy(EntityType<X> et, T att, Descriptor attributeDescriptor, EntityMappingHelper mapper) {
35: this.et = et;
36: this.attribute = att;
37: this.attributeDescriptor = attributeDescriptor;
38: this.mapper = mapper;
39: }
40:
41: static <X> FieldStrategy<? extends FieldSpecification<? super X, ?>, X> createFieldStrategy(
42: EntityType<X> et, FieldSpecification<? super X, ?> att,
43: Descriptor fieldDescriptor, EntityMappingHelper mapper) {
44: if (att instanceof TypesSpecification) {
45: return new TypesFieldStrategy<>(et, (TypesSpecification<? super X, ?>) att, fieldDescriptor, mapper);
46: } else if (att instanceof PropertiesSpecification) {
47: return new PropertiesFieldStrategy<>(et, (PropertiesSpecification<? super X, ?, ?, ?>) att, fieldDescriptor,
48: mapper);
49: }
50: final Attribute<? super X, ?> attribute = (Attribute<? super X, ?>) att;
51: if (attribute.isCollection()) {
52: switch (attribute.getPersistentAttributeType()) {
53: case ANNOTATION:
54: throw new NotYetImplementedException();
55: case DATA:
56: return new PluralDataPropertyStrategy<>(et, (PluralAttribute<? super X, ?, ?>) attribute,
57: fieldDescriptor, mapper);
58: case OBJECT:
59: return createPluralObjectPropertyStrategy(et, (PluralAttribute<? super X, ?, ?>) attribute,
60: fieldDescriptor, mapper);
61: default:
62: break;
63: }
64: } else {
65: switch (attribute.getPersistentAttributeType()) {
66: case ANNOTATION:
67: return new SingularAnnotationPropertyStrategy<>(et, attribute, fieldDescriptor, mapper);
68: case DATA:
69: return new SingularDataPropertyStrategy<>(et, attribute, fieldDescriptor, mapper);
70: case OBJECT:
71: return new SingularObjectPropertyStrategy<>(et, attribute, fieldDescriptor, mapper);
72: default:
73: break;
74: }
75: }
76: // Shouldn't happen
77: throw new IllegalArgumentException();
78: }
79:
80: private static <Y> FieldStrategy<? extends FieldSpecification<? super Y, ?>, Y> createPluralObjectPropertyStrategy(
81: EntityType<Y> et, PluralAttribute<? super Y, ?, ?> attribute,
82: Descriptor descriptor, EntityMappingHelper mapper) {
83: switch (attribute.getCollectionType()) {
84: case LIST:
85: return createOwlListPropertyStrategy(et, (ListAttribute<? super Y, ?>) attribute, descriptor, mapper);
86: case COLLECTION:
87: case SET:
88: return new SimpleSetPropertyStrategy<>(et, attribute, descriptor, mapper);
89: default:
90: throw new NotYetImplementedException(
91: "Unsupported plural attribute collection type " + attribute.getCollectionType());
92: }
93: }
94:
95: private static <Y> FieldStrategy<? extends FieldSpecification<? super Y, ?>, Y> createOwlListPropertyStrategy(
96: EntityType<Y> et, ListAttribute<? super Y, ?> attribute, Descriptor descriptor,
97: EntityMappingHelper mapper) {
98: switch (attribute.getSequenceType()) {
99: case referenced:
100: return new ReferencedListPropertyStrategy<>(et, attribute, descriptor, mapper);
101: case simple:
102: return new SimpleListPropertyStrategy<>(et, attribute, descriptor, mapper);
103: default:
104: throw new NotYetImplementedException(
105: "Unsupported list attribute sequence type " + attribute.getSequenceType());
106: }
107: }
108:
109: void setCascadeResolver(CascadeResolver resolver) {
110: this.cascadeResolver = resolver;
111: }
112:
113: /**
114: * Sets the specified value on the specified instance, the field is taken from the attribute represented by this
115: * strategy. </p>
116: * <p>
117: * Note that this method assumes the value and the field are of compatible types, no check is done here.
118: */
119: void setValueOnInstance(Object instance, Object value) {
120: EntityPropertiesUtils.setFieldValue(attribute.getJavaField(), instance, value);
121: }
122:
123: /**
124: * Extracts the attribute value from the specified instance. </p>
125: *
126: * @return Attribute value, possibly {@code null}
127: */
128: Object extractFieldValueFromInstance(Object instance) throws IllegalAccessException {
129: return EntityPropertiesUtils.getAttributeValue(attribute, instance);
130: }
131:
132: <E> URI resolveValueIdentifier(E instance, EntityType<E> valEt) {
133: URI id = EntityPropertiesUtils.getPrimaryKey(instance, valEt);
134: if (id == null) {
135: id = mapper.generateIdentifier(valEt);
136: EntityPropertiesUtils.setPrimaryKey(id, instance, valEt);
137: }
138: return id;
139: }
140:
141: URI getAttributeContext() {
142: return attributeDescriptor.getContext();
143: }
144:
145: /**
146: * Adds value from the specified axioms to this strategy. </p>
147: * <p>
148: * The value(s) is/are then set on entity field using {@link #buildInstanceFieldValue(Object)}.
149: *
150: * @param ax Axiom to extract value from
151: */
152: abstract void addValueFromAxiom(Axiom<?> ax);
153:
154: /**
155: * Sets instance field from values gathered in this strategy.
156: *
157: * @param instance The instance to receive the field value
158: * @throws IllegalArgumentException Access error
159: * @throws IllegalAccessException Access error
160: */
161: abstract void buildInstanceFieldValue(Object instance) throws IllegalAccessException;
162:
163: /**
164: * Extracts values of field represented by this strategy from the specified instance.
165: *
166: * @param instance The instance to extract values from
167: * @param valueBuilder Builder into which the attribute value(s) are extracted
168: * @throws IllegalArgumentException Access error
169: * @throws IllegalAccessException Access error
170: */
171: abstract void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder)
172: throws IllegalAccessException;
173:
174: /**
175: * Creates property assertion appropriate for the attribute represented by this strategy.
176: *
177: * @return Property assertion
178: */
179: abstract Assertion createAssertion();
180: }